package com.example.vpdemo;

import android.content.Intent;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;


public class FragmentC extends Fragment implements View.OnClickListener {

    View view;

    private ViewPager vp;
    private TabLayout tab;
    ArrayList<Fragment> fragments;
    ArrayList<String> titles;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private FrameLayout fl;
    FragmentC1 fragmentC1;
    FragmentC2 fragmentC2;
    private TextView tv_img;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view= inflater.inflate(R.layout.fragment_c, container, false);
        }
        tv_img = (TextView) view.findViewById(R.id.tv_img);
        tv_img.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        vp = (ViewPager) view.findViewById(R.id.vp);
        tab = (TabLayout) view.findViewById(R.id.tab);

        fl = (FrameLayout) view.findViewById(R.id.fl);
        fragmentC1 = new FragmentC1();
        fragmentC2 = new FragmentC2();
        fragments = new ArrayList<>();
        fragments.add(fragmentC1);
        fragments.add(fragmentC2);
        btn1 = (Button) view.findViewById(R.id.btn1);
        btn2 = (Button) view.findViewById(R.id.btn2);
        btn3 = (Button) view.findViewById(R.id.btn3);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        titles = new ArrayList<>();
        titles.add("C");
        titles.add("D");


        FPAdater fpAdater = new FPAdater(getChildFragmentManager(), fragments, titles);

        vp.setAdapter(fpAdater);
        tab.setupWithViewPager(vp);//与viewpager联系起来



        return view;
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.btn1:
//                addFragment(fragmentC1);
//                break;
//            case R.id.btn2:
//                addFragment(fragmentC2);
//                break;
//            case R.id.btn3:
//                break;
        }

    }

    //向Activity中添加Fragment的方法
    public void addFragment(Fragment fragment) {

        //获得Fragment管理器
        FragmentManager fragmentManager = getChildFragmentManager();
        //使用管理器开启事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //使用事务替换Fragment容器中Fragment对象
        fragmentTransaction.replace(R.id.fl, fragment);
        //提交事务，否则事务不生效
        fragmentTransaction.commit();
    }

}
